@extends('layouts.back_stage')
@section('content')
    <style>
        .s-sell-season,.s-brand,.s-customer,.s-girard,.s-datas{
            display: inline-block;
            width:150px;
            cursor: pointer;
        }
        table,th,td{text-align: center}
        b{color:red;}
        .count_sp{padding-bottom:20px;display:inline-block;}
        .add_order{
            float:right;
        }
        .model_box,.model_box1{
            width:100%;
            height:100%;
            background-color:rgba(0,0,0,0.7);
            position: absolute;
            top:0;
            left:0;
            z-index: 99;
            display: none;
        }
        .model,.model1{
            width:300px;
            height:150px;
            background-color:white;
            border-radius: 10px;
            position: absolute;
            left:50%;
            top:50%;
            margin-left:-150px;
            margin-top:-75px;
        }
        .model p,.model1 p{
            text-align: center;
            margin-top:10px;
            font-size:16px;
        }
        .model select{
            width:200px;
            margin-left:50px;
        }
        .model button,.model1 button{
            margin-top:40px;
        }
        .model-cancel,.model-cancel1{
            margin-left:50px;
        }
        .model-check0,.model-check1{
            margin-left:90px;
        }
        .select2-container{
            margin-left:50px!important;
        }
        .select2-dropdown{
            margin-left:-50px;
        }
        .pull-left .select2-container{
            margin-left:0px!important;
        }
        .send-picking{
            margin-left:5px;
        }
        .hand_count_sp{
            margin-left:10px;
        }
        .send_to{
            margin-left:10px;
        }
        .daterangepicker .ranges{width:100%;}
        .daterangepicker .daterangepicker_start_input label, .daterangepicker .daterangepicker_end_input label{width:28px;}
        .daterangepicker .ranges .range_inputs>div{height:34px;}
    </style>
    <script type="text/javascript">
        var brand = [
            @foreach($brands as $k => $v)[
                {{$k}}, '{{ $v }}'],
            @endforeach
        ]
        var distributor = [
            @foreach($users as $k => $v)[
                {{$k}},'{{ $v }}'],
            @endforeach
        ]
        function getDistributor(user_id){
            for(var i = 0;i < distributor.length;i++){
                if(user_id == distributor[i][0]){
                    return distributor[i][1]
                }
            }
        }
        function getBrand(brand_id) {
            for (var i = 0; i < brand.length; i++) {
                if (brand_id == brand[i][0]) {
                    return brand[i][1]
                }
            }
        }
        (function ($) {
            $.getUrlParam = function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
        })(jQuery);
    </script>
    <link rel="stylesheet" href="{{env('CDN_DOMAIN_NAME')}}/css/commoncss/select2.min.css">
    <link href="{{env('CDN_DOMAIN_NAME')}}/store/commoncss/simplePagination.css" rel="stylesheet">
    <link href="{{env('CDN_DOMAIN_NAME')}}/css/daterangepicker-bs3.css" rel="stylesheet">
    <div class="container">
        <div style="padding:20px 0;"><h3>配货记录</h3></div>
        <div class="pull-left" style="width: 100%;margin-bottom: 20px">
            <label>销售季节：</label>
            <input type="text" class="form-control s-sell-season" placeholder="请输入销售季节" />
            <label>品牌：</label>
            <select name="" class="s-brand form-control">
                <option value="">全部</option>
                @foreach($brands as $k=>$v)
                    <option value="{{$k}}">{{$v}}</option>
                @endforeach
            </select>
            <label>时间：</label>
            <input type="text" readonly class="form-control s-datas" id="reservation" autocomplete="off" autocorrect="off" value="" name="begin_end" placeholder="选择起止时间" style="height:30px;" />
            <label>客户：</label>
            <input type="text" class="form-control s-customer" placeholder="请输入客户姓名">
            <label>款号：</label>
            <input type="text" class="form-control s-girard" placeholder="请输入款号">
            <button onclick="souSan()" class="btn btn-info" style="margin-bottom:4px;">搜索</button>
            <button onclick="reSetScan()" class="btn btn-info" style="margin-bottom:4px;">清空</button>
        </div>
        <span class="count_sp"></span>
        <span class="hand_count_sp"></span>
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>销售季节</th>
                    <th>品牌</th>
                    <th>款号</th>
                    <th>颜色</th>
                    <th>状态</th>
                    <th>配货时间</th>
                    <th>配货人</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
        <div id="paging1" class="page" style=""></div>
        <div class="model_box">
            <div class="model">
                <p>添加配货单</p>
                <select class="js-example-basic-single">
                    <option value="">请选择店铺</option>
                </select>
                <button class="btn btn-info model-check0" style="margin-left:50px;">确定</button>
                <button class="btn btn-info model-cancel" style="margin-left:90px;" onclick="removeModel()">取消</button>

            </div>
        </div>

        <div class="model_box1">
            <div class="model1">
                <p>快递单号</p>
                <input type="text" class="form-control cards" style="width:80%;margin:0 auto;" placeholder="请用扫码枪或手动输入快递单号" value="">
                <button class="btn btn-info model-check1" style="margin-left:50px;" onclick="surecard()">确定</button>
                <button class="btn btn-info model-cancel1" style="margin-left:90px;" onclick="cancard()">取消</button>
            </div>
        </div>
    </div>
    <script src="{{env('CDN_DOMAIN_NAME')}}/js/commonjs/select2.min.js"></script>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/store/commonjs/jquery.simplePagination.js?v0.0.3" type="text/javascript"></script>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/moment.js" type="text/javascript"></script>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/daterangepicker.js" type="text/javascript"></script>
    <script>
        var skip = 0;
        $('#reservation').daterangepicker(null, function(start, end, label) {
            console.log(start.toISOString(), end.toISOString(), label);
        });
        $(document).ready(function(){
            pervNextSerach();
        });
        function souSan(){
            skip=0;
            pervNextSerach();
        }
        function reSetScan(){
            skip = 0;
            $('.s-brand').val("");
            $('.s-sell-season').val("");
            $('.s-girard').val("");
            $('.s-customer').val("");
            pervNextSerach();
        }
        function removeModel(){
            $('.model_box').hide();
        }
        function cancard(){
            $('.model_box1').hide();
        }
        function showModel(){
            $('.model_box').show();
        }
        function returnOrderStatus(type){
            if(type == 0){
                return "<td class='order_status' style='color:skyblue'>配货完成</td>"
            }else if(type == 1){
                return "<td class='order_status' style='color:green'>正在配货</td>"
            }
        }
        function pervNextSerach(){
            var season = $('.s-sell-season').val();
            var brand = $('.s-brand').val();
            var username = $('.s-customer').val();
            var girard = $('.s-girard').val();
            var recode_type = $.getUrlParam('type');
            var start_at = $('#reservation').val().substr(0,10);
            var end_at = $('#reservation').val().substr(13,10);
            $.ajax({
                url:'/picking/distribution-record-data',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type:'get',
                dataType:'json',
                data:{
                    type:recode_type,
                    start_at:start_at,
                    end_at:end_at,
                    season:season,
                    girard:girard,
                    username:username,
                    brand_id:brand,
                    skip:skip
                },
                success:function(res){
                    if(res.result.success == true){
                        var trData = '';
                        $.each(res.content.data,function(idx,obj){
                            trData += '<tr>'+
                                '<td>'+(idx+1)*res.content.skip+'</td>'+
                                '<td>'+obj.season+'</td>'+
                                '<td>'+getBrand(obj.brand_id)+'</td>'+
                                '<td>'+obj.girard+'</td>'+
                                '<td>'+obj.color+'</td>'+
                                returnOrderStatus(obj.status)+
                                '<td>'+obj.created_at+'</td>'+
                                '<td>'+getDistributor(obj.user_id)+'</td>'+
                                '<td><a class="btn btn-info" href="/picking/distribution-record-details?id='+obj.id+'" target="_blank">查看</a><a class="btn btn-info send_to" href="/picking/export-order-to-YiYou?picking_id='+obj.id+'&type='+recode_type+'" target="_blank">导出</a></td>'+
                            '</tr>'
                        })
                        skip = res.content.skip;
                        $('.count_sp').text('共'+res.content.count+'条数据');
                        if(recode_type == 1){
                            $('.hand_count_sp').text('共'+res.content.nums_count+'手');
                        }else if(recode_type == 2){
                            $('.hand_count_sp').text('共'+res.content.nums_count+'件');
                        }
                        $("#paging1").pagination({
                            items: res.content.count,
                            itemsOnPage: 15,
                            cssStyle: 'light-theme',
                            currentPage:res.content.skip
                        });
                        $('.table tbody').html(trData);
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(res.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            });
        }
        $('.js-example-basic-single').select2({
            minimumResultsForSearch:-1,
            ajax: {
                url: "/picking/get-shop-list",
                dataType: 'json',
                delay: 250,
                data: function (params) {
                return {
                    // girard:$('.girard').val()
                };
                },
                processResults: function (data) {
                    $('.js-example-basic-single').select2('data',null);
                    if(data.result.success == true){
                        $.each(data.content,function(idx,obj){
                            var returnText = data.content[idx].name;
                            delete(data.content[idx].name);
                            data.content[idx].text=returnText;
                        }); 
                        if(data.content[0] == undefined){
                            return {
                                results: data.content
                            };
                        }else{
                            return {
                                results: data.content
                            };
                        }
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        return false;
                    }
                },
                cache: true
            },
            escapeMarkup: function (markup) { return markup; }, 
        });
        $('.model-check0').click(function(){
            var shop_id = $(".js-example-basic-single").select2("val");
            var shop_text = $(".js-example-basic-single").select2("data")[0].text;
            $.ajax({
                url:'/picking/increase-bill-order',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type:'post',
                dataType:'json',
                data:{
                    shop_id:shop_id,
                    shop_name:shop_text,
                    assembly_num:1
                },
                success:function(res){
                    if(res.result.success == true){
                        var data = new Date();
                        $('.table tbody').prepend('<tr>'+
                            '<td>'+shop_text+'</td>'+
                            '<td>'+$('.global_userName').text()+'</td>'+
                            '<td>'+data.getFullYear()+'-'+(data.getMonth()+1)+'-'+data.getDate()+' '+data.getHours()+':'+data.getMinutes()+':'+data.getSeconds()+'</td>'+
                            '<td>1</td>'+
                            '<td>0</td>'+
                            '<td style="color:#7266ba;">正在配货</td>'+
                            '<td><a class="btn btn-info" href="/picking/dispersion-code-details/'+res.content+'">配货</a></td>'+
                        '</tr>');
                        removeModel();
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(res.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                    console.log(res);
                }
            });
        });
        $(document).on('click','.page-link',function(){
            if($(this).attr('class') === "page-link next nextPage"){
                if($(this).parent().attr('class') == 'active'){
                    return false;
                }else{
                    pervNextSerach();
                }
            }else if($(this).attr('class') === "page-link prev prevPage"){
                if($(this).parent().attr('class') == 'active'){
                    return false;
                }else{
                    skip=Number(skip)-2;
                    pervNextSerach();
                }
            }else if($(this).attr('class') === "page-link tcdNumber"){
                skip=Number($(this).text())-1;
                pervNextSerach();
            }
        })
        $(document).on('click','.send-picking',function(){
            $('.model_box1').show();
            var id =$(this).attr('id');
            $(".model-check1").attr("abc",id);
        });
        function surecard(){
            var express_num=$(".cards").val();
            if ( !(/^[0-9a-zA-Z]+$/.test( $(".cards").val() )) ) {
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("快递单号为纯数组或者数字英文组合");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            var id=$(".model-check1").attr("abc");
            $.ajax({
                url:'/picking/deliver-goods',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type:'post',
                dataType:'json',
                data:{
                    order_id:id,
                    express_num:express_num
                },
                success:function(res){
                    if(res.result.success == true){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("发货成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        $('.model_box1').hide();
                        $(".fa"+id).parent().siblings('.tr_status').text('已发货');
                        $(".fa"+id).parent().siblings('.tr_status').css({"color":"#1aad1a"});
                        $(".fa"+id).remove();
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(res.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        }
        function getComment(comment){
            if(comment == "" || !comment){
                return '-'
            }else{
                return comment
            }
        }
    </script>
@endsection